/* eslint-disable no-undef */
import Vue from 'vue'
import Router from 'vue-router'
import auth from '@/utils/auth'
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import My from '@/pages/My'
import Putforward from '@/pages/Putforward'
import Putrelease from '@/pages/Putrelease'
import Deposit from '@/pages/Deposit'
import Group from '@/pages/My/Group'
import Root from '@/pages/Root'
import Rule from '@/pages/My/Rule'
import DepositRecord from '@/pages/My/DepositRecord'
import DepositExplain from '@/pages/My/DepositExplain'


Vue.use(Router)

const router = new Router({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes: [{
      path: '/',
      name: 'Home',
      component: Home,
      meta: {
        requiresAuth: true
      },

      children: [{
          path: '/',
          name: 'My',
          component: My,
          meta: {
            title: "个人中心"
          },
        },
        {
          path: 'putforward',
          name: 'Putforward',
          component: Putforward,
          meta: {
            title: "提现记录"
          }
        },
        {
          path: 'putrelease',
          name: 'Putrelease',
          component: Putrelease,
          meta: {
            title: "锁仓释放"
          }
        },
        {
          path: 'deposit',
          name: 'deposit',
          component: Deposit,
          meta: {
            title: "存款收益"
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        title: "登录"
      }
    },
    {
      path: '/my',
      name: 'Root',
      component: Root,
      requiresAuth: true,
      children: [{
          path: 'group',
          name: 'Group',
          component: Group,
          meta: {
            title: '查看团队'
          }
        },
        {
          path: 'rule',
          name: 'Rule',
          component: Rule,
          meta: {
            title: '释放规则'
          }
        },
        {
          path: 'depositRecord',
          name: 'DepositRecord',
          component: DepositRecord,
          meta: {
            title: '存款记录'
          }
        },
            {
        path: 'depositExplain',
        name: 'DepositExplain',
        component: DepositExplain,
        meta: {
          title: '存款说明'
        }
      }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) { //判断是否有标题
    document.title = to.meta.title
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!auth.loggedIn()) {
      next({
        path: 'login',
        // query: {}
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

// router.afterEach((to, from, next) => {})

export default router